const UserModel = require('../model/userModel')

class UserController {

  async signin(req, res) {
    let { username, password } = req.body;
    //验证用户名的长度
    if (!/\w{6,10}/.test(username)) {
      res.send({ code: -1, message: "用户名长度应该是6-10" })
      return;
    }

    //验证用户的唯一性
    let user = await UserModel.findOne({ username });
    if (user) {
      console.log(user)
      res.send({ code: -1, message: "用户名重复" })
      return;
    }
    //数据库入库
    let result = await UserModel.save({ username, password })
    console.log(result)
    if (result) {
      res.send({ code: 1, message: "用户注册成功", username })
    } else {
      res.send({ code: -1, message: "用户注册失败" })
    }
  }
}

module.exports = new UserController();